package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallKeyword;
import org.linlinjava.litemall.db.service.LitemallKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 关键字 管理
 */
@RestController
@RequestMapping("/admin/keyword")
@Validated
public class AdminKeywordController {
    private final Log log = LogFactory.getLog(AdminKeywordController.class);

    @Autowired
    private LitemallKeywordService keywordService;

    /**
     * 显示关键字。写死的8条
     * @param keyword
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/word")
    public Object keyWordlist(String keyword,
                              @Sort @RequestParam(defaultValue = "count") String sort,
                              @Order @RequestParam(defaultValue = "desc") String order) {

        String url="";
        Integer page=1;
        Integer limit=8;

        List<LitemallKeyword> keywordList = keywordService.querySelective(keyword, url,page, limit, sort, order);
        //如果关键字，不存在，存入数据库，以后出现。查询次数 count +1
        if(keywordList.size()<=0){
            LitemallKeyword litemallKeyword = new LitemallKeyword();
            litemallKeyword.setKeyword(keyword);
            litemallKeyword.setCount(1);
            keywordService.add(litemallKeyword);
        }else{
            System.out.println("有关键词。。。进入>>>>>>");
            LitemallKeyword oneByKeyword = keywordService.findOneByKeyword(keyword);
            oneByKeyword.setCount(oneByKeyword.getCount()+1);
            int i = keywordService.updateById(oneByKeyword);
        }

        List<String> keywordVOList=new ArrayList<>();
        for (LitemallKeyword litemallKeyword : keywordList) {
            String beforeKey = litemallKeyword.getKeyword();
            keywordVOList.add(beforeKey);
        }
        return ResponseUtil.okList(keywordVOList);
    }


    @RequiresPermissions("admin:keyword:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "查询")
    @GetMapping("/list")
    public Object list(String keyword, String url,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallKeyword> keywordList = keywordService.querySelective(keyword, url, page, limit, sort, order);
        return ResponseUtil.okList(keywordList);
    }

    private Object validate(LitemallKeyword keywords) {
        String keyword = keywords.getKeyword();
        if (StringUtils.isEmpty(keyword)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @RequiresPermissions("admin:keyword:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallKeyword keyword) {
        Object error = validate(keyword);
        if (error != null) {
            return error;
        }
        keywordService.add(keyword);
        return ResponseUtil.ok(keyword);
    }

    @RequiresPermissions("admin:keyword:read")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        LitemallKeyword keyword = keywordService.findById(id);
        return ResponseUtil.ok(keyword);
    }

    @RequiresPermissions("admin:keyword:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallKeyword keyword) {
        Object error = validate(keyword);
        if (error != null) {
            return error;
        }
        if (keywordService.updateById(keyword) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(keyword);
    }

    @RequiresPermissions("admin:keyword:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallKeyword keyword) {
        Integer id = keyword.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        keywordService.deleteById(id);
        return ResponseUtil.ok();
    }

}
